package com.yiwu.common.aspect;

import com.yiwu.common.enums.ResponseCode;
import com.yiwu.common.exception.BusinessException;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.logging.log4j.util.Strings;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

/**
 * @version 1.0
 * @description TODO
 * @date 2024/5/26 18:42
 */
@Aspect
@Component
public class ApiVerifyLoginAspect {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Before("@annotation(com.yiwu.common.aspect.ApiVerifyLogin)")
    public void beforeAddProduct(JoinPoint joinPoint) {

        String authHeader = request.getHeader("Authorization");
        if (authHeader == null) throw new BusinessException(ResponseCode.NO_LOGIN);
        String token = stringRedisTemplate.opsForValue().get(authHeader + "yiwu");
        if (Strings.isEmpty(token)) throw new BusinessException(ResponseCode.TOKEN_ERROR);
    }


}
